from django.db import models
from django.contrib.auth.models import AbstractUser
import order
import store



class User(AbstractUser):
    user_grade = (
        (0, '普卡会员'),
        (1, '金卡会员'),
        (2, '白金卡会员'),
        (3, '钻石会员'),
    )
    mobile = models.CharField(max_length=11,null=True,verbose_name='手机号')
    birthday = models.DateTimeField(null=True)
    grade = models.SmallIntegerField(
        choices=user_grade,
        default=0,
        verbose_name='会员等级',
    )
    growth = models.IntegerField(
        default=0,
        verbose_name='成长值',
    )

    license_time = models.DateTimeField(
        auto_now_add=True,
        verbose_name='获取驾照时间',
    )

    is_certification = models.BooleanField(
        default=False,
        verbose_name='是否实名',
    )

    money = models.IntegerField(
        default=0,
        verbose_name='余额',
    )
    icon = models.ImageField(
        upload_to='icon',
        default='icon/default.jpg',
        verbose_name='头像'
    )

    @property
    def user_grade_get(self):
        return self.get_grade_display()

    #我的违章
    @property
    def traffic_get(self):
        traffic_list = []
        for traffic in self.traffic_id.all().values('record'):
            traffic_list.append(traffic.get('record'))
        return traffic_list

    #紧急联系人
    @property
    def em_person_get(self):
        return self.user_em_person.em_name



    class Meta:
        db_table = 'car_rent_user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username



class Emergency(models.Model):
    user = models.OneToOneField(
        User,
        related_name='user_em_person',
        on_delete=models.SET_NULL,
        db_constraint=False,
        verbose_name='紧急联系人',
        null=True
    )
    em_name = models.CharField(
        max_length=8,
        verbose_name='紧急联系人姓名',
    )
    mobile = models.CharField(
        max_length=11,
        unique=True,
    )

    class Meta:
        db_table = 'emergency_person'
        verbose_name_plural = '紧急联系人表'

    def __str__(self):
        return self.em_name


class Card(models.Model):
    user_id = models.ForeignKey(
        User,
        related_name='card_id',
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name='用户编号'
    )
    card_name = models.CharField(
        max_length=8,
        verbose_name='证件名称'
    )
    is_login = models.BooleanField(
        default=False,
        verbose_name="是否登录账号",
    )
    card_num = models.CharField(
        max_length=8,
        null=True,
        verbose_name='证件号码'
    )
    class Meta:
        db_table = 'user_card'
        verbose_name_plural = '证件表'

    def __str__(self):
        return self.card_name

class Question(models.Model):

    user_id = models.ForeignKey(
        User,
        related_name='question_id',
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="用户编号",

    )
    title = models.CharField(
        max_length=32,
        verbose_name='安全问题'
    )

    answer = models.CharField(
        max_length=32,
        verbose_name='问题答案'
    )

    class Meta:
        db_table = 'user_question'
        verbose_name_plural = '安全问题表'

    def __str__(self):
        return self.title

#一个订单对应一张发票
class Receipt(models.Model):

    user = models.ForeignKey(
        User,
        related_name='receipt_user',
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name='用户发票'
    )
    order = models.OneToOneField(
        'order.Order',
        related_name='user_receipt',
        on_delete=models.SET_NULL,
        db_constraint=False,
        verbose_name='订单发票',
        null=True
    )
    invoice = models.CharField(
        max_length=64,
        verbose_name='发票抬头',
    )
    address_name = models.CharField(
        max_length=16,
        verbose_name='收件人姓名',
    )
    mobile = models.CharField(
        max_length=11,
        unique=True,
        verbose_name='手机号',
    )
    zip = models.IntegerField(
        null=True,
        verbose_name='邮编'
    )
    addr = models.CharField(
        max_length=256,
        verbose_name='收件地址'
    )

    class Meta:
        db_table = 'user_receipt'
        verbose_name_plural = '发票表'

    def __str__(self):
        return f'{self.address_name}的发票'

class Traffic(models.Model):
    user_id = models.ForeignKey(
        User,
        related_name='traffic_id',
        db_constraint=False,
        on_delete=models.CASCADE,
        verbose_name='用户编号'
    )
    create_time = models.DateTimeField(
        auto_now_add=True,
        verbose_name='违章时间'
    )
    record = models.TextField(verbose_name='违章记录')

    class Meta:
        db_table = 'tb_traffic'
        verbose_name_plural = "违章表"

    def __str__(self):
        return f'{self.user_id.username}的违章'

class Complain(models.Model):

    user_id = models.ForeignKey(
        User,
        related_name='complain_id',
        db_constraint=False,
        on_delete=models.CASCADE,
        verbose_name='用户编号'
    )
    user_name = models.CharField(
        max_length=16,
        verbose_name='客户姓名'
    )
    num = models.CharField(
        max_length=11,
        unique=True,
        verbose_name='联系方式'
    )
    #投诉只能投一个订单。和订单表是一对一的关系
    order_id = models.OneToOneField(
        'order.Order', #解决表在另一个app下，但是需要导入的问题
        related_name='complain_id',
        db_constraint=False,
        on_delete=models.DO_NOTHING,
        verbose_name='订单编号',
    )
    #前台输入card_num, 根据user_id反向查到card表，然后取num属性得到一个num列表，
    # 再判断car_num是否在num列表中，存在就继续进行，不存在，就说明card_num输入错误


    # card_id = models.ForeignKey(
    #     Card,
    #     related_name='complain_id',
    #     db_constraint=False,
    #     on_delete=models.DO_NOTHING,
    #     verbose_name='证件号码',
    # )
    # 再加一个门店表
    store_id = models.ForeignKey(
        'store.Store',
        related_name='complain_id',
        db_constraint=False,
        on_delete=models.DO_NOTHING,
        verbose_name='门店编号',
    )
    text = models.TextField(
        verbose_name='投诉内容'
    )

    class Meta:
        db_table = 'user_complain'
        verbose_name_plural = "投诉表"

    def __str__(self):
        return f'{self.user_name}的投诉'





